package com.example.controller;

import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    public EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
    Emp e =  empService.login(emp);
    if(e != null){
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("id", e.getId());
        datamap.put("username",e.getUsername());
        String jwt = JwtUtils.generateJwt(datamap);
        return Result.success(jwt);
    }

        else
        return Result.error("账号或密码错误");

    }
}